<?php

namespace app\admin\controller;

use app\common\model\Video as VideoModel;
use app\common\model\VideoCate;

class Video extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 分页获取班级信息
     * @return void
     */
    public function list()
    {
        //查询条件
        $where = array();

        if (input('cate_id')) {
            $where['cate_id'] = input('cate_id');
        }
        if (input('title')) {
            $where['title'] = array('like', "%" . input('title') . "%");
        }

        //视频分类
        $cate = VideoCate::getColumn([], 'id,name');

        //分页信息
        $page = max(input('page'), 1);
        $size = input('limit', 20);

        //获取列表
        $list = VideoModel::getPage($where, $page, $size, 'id desc');

        //总页码
        $total = VideoModel::getCount($where);

        //数据返回
        if (!$list) {
            return returnJson(0, '', array('list' => [], 'total' => 0));
        } else {
            foreach ($list as $k => $v) {
                $list[$k]['picture'] = tomedia($v['picture']);
                $list[$k]['cate_name'] = $cate[$v['cate_id']];
            }

            return returnJson(0, '', [
                'list' => $list,
                'size' => $size,
                'total' => $total
            ]);
        }

    }

    /**
     * 视频信息
     * @return \think\response\Json
     */
    public function detail()
    {
        //查询条件
        $where = array('id' => array('eq', input('id')));

        //获取信息
        $detail = VideoModel::getOne($where);

        $detail['picture'] = tomedia($detail['picture']);
        $detail['video'] = tomedia($detail['video']);

        //返回信息
        return returnJson(0, '', $detail);
    }

    /**
     * 新增或编辑视频
     * @return \think\response\Json
     */
    public function update()
    {
        $input = input('');
        $data = array(
            'title' => $input['title'],
            'desc' => $input['desc'],
            'picture' => $input['picture'],
            'ord' => $input['ord'],
            'video' => $input['video'],
            'content' => $input['content'],
            'status' => $input['status'],
            'cate_id' => $input['cate_id'],
            'rate' => $input['rate'],
            'update_time' => time(),

        );
        if ($input['id']) {
            $data['id'] = $input['id'];
            $video = VideoModel::getOne(['id' => $data['id']]);
        }
        if ($data['id']) {
            if ($video['picture'] != $input['picture']) {
                model('qiniu')->remove($video['picture']);//删除图集
            }
            if ($video['video'] != $input['video']) {
                model('qiniu')->remove($video['video']);//删除视频
            }
            $res = VideoModel::update($data,array('id'=>$data['id']));
        } else {
            $data['create_time'] = time();
            $res = VideoModel::create($data);
        }
        if ($res) {
            return returnJson(0, '操作成功');
        }
        return returnJson(1, '网络异常，请重试');
    }

    /**
     * 删除视频
     * @return \think\response\Json
     */
    public function delete()
    {
        if (input('id', 0)) {
            //查询条件
            $where['id'] = input('id');

            //视频信息
            $video = VideoModel::getOne($where);

            //删除七牛云存储的视频主图和视频
            model('qiniu')->remove($video['picture']);//删除图集
            model('qiniu')->remove($video['video']);//删除视频

            //删除记录
            VideoModel::setDel($where);
        }
        return returnJson(0, '');

    }

    /**
     * 分类列表
     * @return \think\response\Json
     */
    public function cate()
    {

        //获取信息
        $list = VideoCate::getAll([], 'ord desc');

        //返回信息
        return returnJson(0, '', $list);
    }


    /**
     * 新增或修改视频分类
     * @return \think\response\Json|void
     */
    public function editCate()
    {
        if (request()->isPost()) {
            $input = input();
            $time = time();
            $data = array(
                'name' => $input['name'],
                'ord' => $input['ord'],
                'update_time' => $time,
            );
            if ($input['id']) {
                $data['id'] = $input['id'];
            } else {
                $data['create_time'] = $time;
            }

            if ($data['id']) {
                $res = VideoCate::update($data, array('id' => $data['id']));
            } else {
                $res = VideoCate::setInsertGetId($data);
            }
            if ($res) {
                return returnJson(0, '操作成功');
            }
            return returnJson(1, '网络异常，请重试');
        }
    }

    /**
     * 删除分类
     * @return \think\response\Json
     */
    public function delCate()
    {
        if (input('id', 0)) {
            //查询条件
            $where['id'] = input('id');
            VideoCate::setDel($where);
        }
        return returnJson(0, '');

    }


}
